package com.example.intenttrial;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

public class MainActivity extends Activity implements OnClickListener{

	LayoutInflater inflater;
	
	private RelativeLayout rl1;
	private RelativeLayout rl2;
	private RelativeLayout rl3;
	HorizontalScrollView hsv;
	
	int width;
	int height;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		System.out.println("onCreate()");

		inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		hsv = (HorizontalScrollView)inflater.inflate(R.layout.activity_main, null);
		setContentView(hsv);

		final int w = hsv.getMeasuredWidth();
        final int h = hsv.getMeasuredHeight();
		
		
		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		width = metrics.widthPixels;
		height = metrics.heightPixels; 
		
		System.out.println("height is " + metrics.heightPixels);
		System.out.println("width is " + metrics.widthPixels);
		mapUI();
		
		
		hsv.scrollBy(width * 2, 0);
		
		
		 new Handler().post(new Runnable() {
             @Override
             public void run() {
                 hsv.scrollBy(width * 2, 0);
             }
         });
		
	}
	
	

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);
		System.out.println("onConfigurationChanged()");
	}



	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		System.out.println("onPause()");
	}



	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
		System.out.println("onRestart()");
	}



	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		System.out.println("onResume()");
	}



	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		System.out.println("onStart()");
	}



	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		System.out.println("onStop()");
	}



	private void mapUI() {
		rl1 = (RelativeLayout)inflater.inflate(R.layout.rl_scrollitem, null);
		rl2 = (RelativeLayout)inflater.inflate(R.layout.rl_scrollitem, null);
		rl3 = (RelativeLayout)inflater.inflate(R.layout.rl_scrollitem, null);
		
		rl1.setBackgroundColor(Color.RED);
		
		rl2.setBackgroundColor(Color.YELLOW);
		rl3.setBackgroundColor(Color.GREEN);
		
//		ArrayList<View> children = new ArrayList<View>();
//		children.add(rl1);
//		children.add(rl2);
//		children.add(rl3);
		Button b1 = new Button(getApplicationContext());
		Button b2 = new Button(getApplicationContext());
		Button b3 = new Button(getApplicationContext());
		b1.setBackgroundColor(Color.RED);
		b1.setText("Button");
		b1.setWidth(width - 50);
		
		b1.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				hsv.scrollBy(width - 50, 0);
			}
		});
		
		b2.setBackgroundColor(Color.YELLOW);
		b2.setText("Button");
		b2.setWidth(width);
		b3.setBackgroundColor(Color.GREEN);
		b3.setText("Button");
		b3.setWidth(width);
//		
//		ViewGroup parent = (ViewGroup) hsv.getChildAt(0);
		LinearLayout parent = (LinearLayout)findViewById(R.id.top);
		
        parent.addView(b1);
        parent.addView(b2);
        parent.addView(b3);
        parent.addView(rl1);
        
        LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams)rl1.getLayoutParams();
		params1.width = width;
		params1.height = height;
		rl1.setLayoutParams(params1);
		
//		hsv.addChildrenForAccessibility(children);
//		parent.addView(rl1);
//		parent.addView(rl2);
//		parent.addView(rl3);
		
//		for (int i = 0; i < children.size(); i++) {
//            children.get(i).setVisibility(View.INVISIBLE);
//            parent.addView(children.get(i));
//        }
		
//		hsv.addView(rl2);
//		hsv.addView(rl3);
		
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}

}
